package com.flong.common;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;

import com.flong.GlobalConstants;

/**
 * @Description httpclient客户端请求处理的通用类  
 * @ClassName   HttpClientUtls  
 * @Date        2017年6月6日 上午11:41:36  
 * @Author      liangjilong  
 * @Copyright (c) All Rights Reserved, 2017.
 */
@SuppressWarnings("all")
public class HttpClientUtil {

	private static Logger logger = org.slf4j.LoggerFactory.getLogger(HttpClientUtil.class);  
	private final static int CONNECT_TIMEOUT = 7000; // in milliseconds
    private final static String DEFAULT_ENCODING = "UTF-8";
    private static RequestConfig requestConfig;  
    private static final int MAX_TIMEOUT = 7000;  
    private static PoolingHttpClientConnectionManager connMgr;  
    private static String LINE = System.getProperty("line.separator");//换行相当于\n
    
    static {  
        // 设置连接池  
        connMgr = new PoolingHttpClientConnectionManager();  
        // 设置连接池大小  
        connMgr.setMaxTotal(100);  
        connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal());  
  
        RequestConfig.Builder configBuilder = RequestConfig.custom();  
        // 设置连接超时  
        configBuilder.setConnectTimeout(MAX_TIMEOUT);  
        // 设置读取超时  
        configBuilder.setSocketTimeout(MAX_TIMEOUT);  
        // 设置从连接池获取连接实例的超时  
        configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);  
        // 在提交请求之前 测试连接是否可用  
        configBuilder.setStaleConnectionCheckEnabled(true);  
        requestConfig = configBuilder.build();  
    }
    
	
	/**
	 * 
     * @Description 获取参数内容  
     * @Author      liangjilong  
     * @Date        2017年6月6日 上午11:36:50  
     * @param params
     * @return
     * @throws UnsupportedEncodingException 参数  
     * @return String 返回类型   
     * @throws
	 */
	public static String getConcatParams(Map<String, Object> params) throws UnsupportedEncodingException {
        String content = null;
        Set<Entry<String,Object>> set = params.entrySet();//Map.entrySet 方法返回映射的 collection 视图，其中的元素属于此类
        StringBuilder sb = new StringBuilder();
        for(Entry<String,Object> i: set){
        	//将参数解析为"name=tom&age=21"的模式
            sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue().toString(), "utf-8")).append("&");
        }
        if(sb.length() > 1){
            content = sb.substring(0, sb.length()-1);
        }
        return content;
    }
	
	
	
	 /**
     * @Description 拼接请求参数  
     * @Author      liangjilong  
     * @Date        2017年5月24日 上午10:39:28  
     * @param @return 参数  
     * @return String 返回结果如：  userName=1111&passWord=222  
     * @throws
     */
	private static String getContent(Map<String, Object> params, String encoding) throws UnsupportedEncodingException {
        String content = null;
        Set<Entry<String,Object>> set = params.entrySet();//Map.entrySet 方法返回映射的 collection 视图，其中的元素属于此类
        StringBuilder sb = new StringBuilder();
        for(Entry<String,Object> i: set){
        	//将参数解析为"name=tom&age=21"的模式
            sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue().toString(), encoding)).append("&");
        }
        if(sb.length() > 1){
            content = sb.substring(0, sb.length()-1);
        }
        return content;
    }
	
	public static String post(String postUrl, Map<String, Object> params, String encoding){
		StringBuffer sb = new StringBuffer();
		String line;
		try {
			URL url = new URL(postUrl);
			URLConnection urlConn = url.openConnection();
			HttpURLConnection httpUrlConn = (HttpURLConnection)urlConn;
			
			httpUrlConn.setDoOutput(true);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);
			
			httpUrlConn.setRequestMethod("POST");
			OutputStreamWriter wr = new OutputStreamWriter(httpUrlConn.getOutputStream());
			String content = getContent(params, encoding);
			wr.write(content);
			wr.flush();
			BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConn.getInputStream(), encoding));
			while((line = in.readLine()) != null){
				sb.append(line);
			}
			wr.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
    }
	
	
	public static String get(String rURL, String encoding) {
		String result = "";
		
		try {
			URL url = new URL(rURL);
			
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setReadTimeout(180000);//将读超时设置为指定的超时，以毫秒为单位。用一个非零值指定在建立到资源的连接后从 Input 流读入时的超时时间。如果在数据可读取之前超时期满，则会引发一个 java.net.SocketTimeoutException。
			con.setDoInput(true);//指示应用程序要从 URL 连接读取数据。
			con.setRequestMethod("GET");//设置请求方式
			if(con.getResponseCode() == 200){//当请求成功时，接收数据（状态码“200”为成功连接的意思“ok”）
				InputStream is = con.getInputStream();
				result = formatIsToString(is, encoding);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.trim();
	}
	
	/**
	 * 
     * @Description 格式字符串  
     * @Author      liangjilong  
     * @Date        2017年6月6日 上午11:39:23  
     * @param is
     * @param encoding
     * @return
     * @throws Exception 参数  
     * @return String 返回类型   
     * @throws
	 */
	public static String formatIsToString(InputStream is, String encoding) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        try {
            while( (len=is.read(buf)) != -1){
                baos.write(buf, 0, len);
            }
            baos.flush();
            baos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(baos.toByteArray(), encoding);
    }
	
	
	  /**
     * @Description HttpUrlConnection  
     * @Author      liangjilong  
     * @Date        2017年5月17日 上午10:53:00  
     * @param @param urlStr
     * @param @param data
     * @param @param contentType
     * @param @param requestMethod
     * @param @return 参数  
     * @return String 返回类型   
     * @throws
     */
 	public static String createHttp(String reqUrl, String reqBodyParams, String contentType,String requestMethod){
 		
 		BufferedReader reader = null;
 		HttpURLConnection  conn =null;
         try {
             URL url = new URL(reqUrl);
             conn = (HttpURLConnection) url.openConnection();  
             conn.setDoOutput(true);
             conn.setDoInput(true);
             conn.setUseCaches(false);
             conn.setConnectTimeout(CONNECT_TIMEOUT);
             conn.setReadTimeout(CONNECT_TIMEOUT);
             conn.setRequestMethod(requestMethod);
             conn.connect();  
             InputStream inputStream = conn.getInputStream();
             if(contentType != null){
            	 conn.setRequestProperty("Content-type", contentType);
             }
             if(reqBodyParams!=null){
            	 OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
            	 writer.write(reqBodyParams); 
            	 writer.flush();
            	 writer.close();  
             }
             reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_ENCODING));
             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = reader.readLine()) != null) {
                 sb.append(line);
                 sb.append("\r\n");//\r是回车\n是换行
             }
             logger.info("请求链接为："+reqUrl+"返回的数据为"+sb.toString());
             return sb.toString();
             
         } catch (IOException e) {
             logger.error("Error connecting to " + reqUrl + ": " + e.getMessage());
         } finally {
             try {
                 if (reader != null)
                     reader.close();
                 if (conn != null)
                	 conn.disconnect();
             } catch (IOException e) {
             	  logger.error("Error connecting to finally" + reqUrl + ": " + e.getMessage());
             }
         }
         return null;
     }
 	
  
 	/**
 	 * 
     * @Description 建立http请求链接支持SSL请求
     * @Author      liangjilong  
     * @Date        2017年6月6日 上午11:11:56  
     * @param requestUrl
     * @param requestMethod
     * @param outputStr
     * @param headerMap请求头属性，可以为空
     * @param isSsl 当isSSL=true的时候支持Https处理，当isSSL=false的时候http请求
     * @param sslVersion  支持https的版本参数
     * @return 参数  
     * @return String 返回类型   
     * @throws
 	 */
	public static String createHttps(String requestUrl, String requestMethod,Map<String,Object> headerMap,
			boolean isSsl,String sslVersion,String bodyParams) {
		HttpsURLConnection conn = null ;
		BufferedReader bufferedReader =null;
		InputStreamReader inputStreamReader =null; 
		InputStream inputStream = null;
		try {
			SSLSocketFactory ssf = null;
			if(isSsl){
				//这行代码必须要在创建URL对象之前，因为先校验SSL的https请求通过才可以访问http
				ssf = SSLContextSecurity.createIgnoreVerifySSL(sslVersion);
			}
			// 从上述SSLContext对象中得到SSLSocketFactory对象
			URL url = new URL(requestUrl);
		 
			conn = (HttpsURLConnection) url.openConnection();
			if(isSsl){
				conn.setSSLSocketFactory(ssf);
			}
			conn.setDoOutput(true);//输出
			conn.setDoInput(true);//输入
			conn.setUseCaches(false);//是否支持缓存
			
			/*设置请求头属性和值 */
			if(headerMap!=null && !headerMap.isEmpty()){
				 for (String key : headerMap.keySet()) {
					 Object value = headerMap.get(key);
					 //如：conn.addRequestProperty("Authorization","123456");
					 conn.addRequestProperty(key,String.valueOf(value));
				 }
			}
	        
			// 设置请求方式（GET/POST）
			conn.setRequestMethod(requestMethod);
			// 当设置body请求参数
			if (!ObjectUtil.isEmpty(bodyParams)) {  
	        	DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());  
	            outStream.write(bodyParams.getBytes("UTF-8"));  
	            outStream.close();
	            outStream.flush(); 
	        } 

			if(conn!=null && conn.getResponseCode()==200){
				// 从输入流读取返回内容
				inputStream = conn.getInputStream();
				inputStreamReader= new InputStreamReader(inputStream, GlobalConstants.UTF8);
				bufferedReader = new BufferedReader(inputStreamReader);
				String str = null;
				StringBuffer buffer = new StringBuffer();
				while ((str = bufferedReader.readLine()) != null) {
					buffer.append(str);
					buffer.append("\r").append(LINE);
				}
				return buffer.toString();
			}else{
				return "FAIL";
			}
		} catch (ConnectException ce) {
			logger.error("连接超时：{}",  ce+"\t请求链接"+requestUrl);
		} catch (Exception e) {
			logger.error("https请求异常：{}", e+"\t请求链接"+requestUrl);
			return "FAIL";//请求系统频繁
		}finally{
			// 释放资源
			try {
				if(conn!=null)
					conn.disconnect();
				if(bufferedReader!=null)
					bufferedReader.close();
				
				if(inputStreamReader!=null)
					inputStreamReader.close();
				
				if(inputStream!=null)
					inputStream.close();
					inputStream = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "";
	}
	
	
	 /** 
     * 发送 POST 请求（HTTP），JSON形式 
     * @Author      liangjilong  
     * @Date        2017年5月22日 下午3:57:03  
     * @param @param apiUrl
     * @param @param json
     * @param @param contentType
     * @param @return 参数  
     * @return String 返回类型   
     * @throws
     */
    public static String httpClientPost(String reqUrl, Object reqBodyParams,String contentType,String encoding,Map<String,Object> headerMap) {  
    	String  result ="";  
        CloseableHttpClient httpClient = HttpClients.createDefault();  
        HttpPost httpPost = new HttpPost(reqUrl);  
        
        /*设置请求头属性和值 */
		if(headerMap!=null && !headerMap.isEmpty()){
			 for (String key : headerMap.keySet()) {
				 Object value = headerMap.get(key);
				 //如： httpPost.addHeader("Content-Type", "application/json");  
				 httpPost.addHeader(key,String.valueOf(value));
			 }
		}
       
        CloseableHttpResponse response = null;  
        try {  
            httpPost.setConfig(requestConfig);  
            if(reqBodyParams!=null){
            	logger.info(getCurrentClassName()+".httpClientPost方法返回的参数为："+reqBodyParams.toString());
            	StringEntity stringEntity = new StringEntity(reqBodyParams.toString(),(encoding==null||"".equals(encoding)?"utf-8":encoding));//解决中文乱码问题  
            	if(encoding!=null && encoding!=""){
            		stringEntity.setContentEncoding(encoding);  
            	}
            	if(contentType!=null && contentType!=""){
            		stringEntity.setContentType(contentType);  
            	}
            	httpPost.setEntity(stringEntity);  
            	
            }
            response = httpClient.execute(httpPost);  
            HttpEntity entity = response.getEntity();  
            if (entity != null && response.getStatusLine().getStatusCode()==200) {
            	//Attempted read from closed stream,因为EntityUtils.toString(HttpEntity)方法被使用了多次。所以每个方法内只能使用一次。
            	//httpStr = EntityUtils.toString(entity, "UTF-8");  
            	String buffer = IoUtil.getInputStream(entity.getContent());
                logger.info("HttpUrlPost的entity返回的信息为："+buffer.toString());
                return buffer.toString();//返回  
            }else{
            	logger.error("HttpUrlPost的entity对象为空");
            	return result;
            }
        } catch (IOException e) {
        	logger.error("HttpUrlPost出现异常，异常信息为:"+e.getMessage());
            e.printStackTrace();  
        } finally { 
        	 if(response != null){  
                 try {  
                	 response.close();  
                 } catch (IOException e) {  
                     e.printStackTrace();  
                 }  
             }  
        	 if(httpClient != null){  
                 try {  
                	 httpClient.close();  
                 } catch (IOException e) {  
                     e.printStackTrace();  
                 }  
             }  
        }  
        return result;  
    }  
    

	/** 
    * @Description  发送 POST 请求（HTTP），JSON形式   
    * @Author      liangjilong  
    * @Date        2017年5月22日 下午3:57:03  
    * @param @param apiUrl
    * @param @param json
    * @param @param contentType
    * @param @return 参数  
    * @return String 返回类型   
    * @throws
    */
   public static String httpClientPost(String reqUrl, Map<String,Object> reqBodyParams,String contentType,String encoding,Map<String,Object> headerMap) {  
   		CloseableHttpClient httpClient = HttpClients.createDefault();  
   		return commonHttpClientPost(reqUrl, reqBodyParams, contentType, encoding, headerMap, httpClient);  
   }  
   
    
    
    /** 
     * @Description httpClientGet  
     * @Author      liangjilong  
     * @Date        2017年5月22日 下午3:57:03  
     * @param @param apiUrl
     * @param @param json
     * @param @param contentType
     * @param @return 参数  
     * @return String 返回类型   
     * @throws
     */
    public static String httpClientGet(String reqUrl, Map<String,Object> reqBodyParams,String contentType,String encoding,Map<String,Object> headerMap) {  
        CloseableHttpClient httpClient = HttpClients.createDefault();  
        String httpStr = commonHttpClientGet(reqUrl, reqBodyParams, encoding,
				headerMap, httpClient);  
        return httpStr;  
    }

    /**
     * @Description reqParamStr  
     * @Author      liangjilong  
	 * @Email       jilongliang@sina.com 
     * @Date        2017年8月1日 上午10:34:25  
     * @param reqBodyParams
     * @param encoding
     * @param reqParamStr
     * @return
     * @throws IOException
     * @throws UnsupportedEncodingException 参数  
     * @return String 返回类型
     */
	private static String reqParamStr(Map<String, Object> reqBodyParams, String encoding, String reqParamStr) throws IOException,
			UnsupportedEncodingException {
		if(reqBodyParams!=null && !reqBodyParams.isEmpty()){
			//封装请求参数  
		    List<NameValuePair> params = new ArrayList<NameValuePair>();
			for (Map.Entry<String, Object> entry : reqBodyParams.entrySet()) {  
				String key = entry.getKey();
				Object val = entry.getValue();
			    params.add(new BasicNameValuePair(key, val.toString())); 
			}
			reqParamStr = EntityUtils.toString(new UrlEncodedFormEntity(params,encoding));  
			//httpGet.setURI(new URIBuilder(httpGet.getURI().toString() + "?" + param).build());  
		}
		return reqParamStr;
	}  

	/**
     * @Description 创建httpClient  
     * @Author      liangjilong  
	 * @Email       jilongliang@sina.com 
     * @Date        2017年8月1日 上午10:26:29  
     * @return 参数  
     * @return CloseableHttpClient 返回类型
	 * @throws Exception 
	 */
	public static CloseableHttpClient createHttpClient() {
		SSLContext sslcontext = null;
		CloseableHttpClient httpClient = null;
		try {
			sslcontext = new SSLContextBuilder().loadTrustMaterial(null, 
			new TrustStrategy() 
			{
				public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
						return true;
					}
			}).build();
		
		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		
		httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
		
		} catch (KeyManagementException e) {
			logger.error("@see "+getCurrentClassName()+"KeyManagementException,"+e.getMessage());
		} catch (NoSuchAlgorithmException e) {
			logger.error("@see "+getCurrentClassName()+"NoSuchAlgorithmException,"+e.getMessage());
		} catch (KeyStoreException e) {
			logger.error("@see "+getCurrentClassName()+"KeyStoreException,"+e.getMessage());
		}
		
		return httpClient;
	}
	 
	/**
     * @Description 创建createHttpsClient  
     * @Author      liangjilong  
	 * @Email       jilongliang@sina.com 
     * @Date        2017年8月1日 上午10:26:29  
     * @return 参数  
     * @return CloseableHttpClient 返回类型
	 * @throws Exception 
	 */
	public static CloseableHttpClient createHttpsClient() {
		SSLContext sslcontext = null;
		CloseableHttpClient httpClient = null;
		
	    PoolingHttpClientConnectionManager phccm = null;
	    
		try {
			sslcontext = new SSLContextBuilder().loadTrustMaterial(null, 
			new TrustStrategy() 
			{
				public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
					return true;
				}
			}).build();
		
		LayeredConnectionSocketFactory  lcsf  = new  SSLConnectionSocketFactory(SSLContext.getDefault());
		
		httpClient = HttpClients.custom().setSSLSocketFactory(lcsf).build();
		Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
				.register("https", lcsf).register("http", new PlainConnectionSocketFactory()).build();
        phccm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        phccm.setMaxTotal(200);
        phccm.setDefaultMaxPerRoute(20);
        
        return HttpClients.custom().setConnectionManager(phccm).build();       
		} catch (KeyManagementException e) {
			return null;
		} catch (NoSuchAlgorithmException e) {
			return null;
		} catch (KeyStoreException e) {
			return null;
		}
	}
	
	/**
     * @Description 创建httpsPost  
     * @Author      liangjilong  
	 * @Email       jilongliang@sina.com 
     * @Date        2017年8月1日 上午10:29:09  
     * @param reqUrl
     * @param reqBodyParams
     * @param contentType
     * @param encoding
     * @param headerMap
     * @return 参数  
     * @return String 返回类型
	 */
	public static String createHttpsPost(String reqUrl, Map<String,Object> reqBodyParams,String contentType,String encoding,Map<String,Object> headerMap) {  
   		CloseableHttpClient httpClient = createHttpClient(); 
   		return commonHttpClientPost(reqUrl, reqBodyParams, contentType, encoding, headerMap, httpClient);  
   }

	/**
    * 
     * @Description commonHttpClientPost  
     * @Author      liangjilong  
	 * @Email       jilongliang@sina.com 
     * @Date        2017年8月1日 上午10:34:43  
     * @param reqUrl
     * @param reqBodyParams
     * @param contentType
     * @param encoding
     * @param headerMap
     * @param httpClient
     * @return 参数  
     * @return String 返回类型
	 */
	private static String commonHttpClientPost(String reqUrl, Map<String, Object> reqBodyParams, String contentType, String encoding, Map<String, Object> headerMap, CloseableHttpClient httpClient) {
		String  result ="";  
   		CloseableHttpResponse response = null;  
    try {  
        HttpPost httpPost = new HttpPost(reqUrl);  
        String reqParamStr = "";
        /*设置请求头属性和值 */
		 if(headerMap!=null && !headerMap.isEmpty()){
			 for (String key : headerMap.keySet()) {
				 Object value = headerMap.get(key);
				 //如： httpPost.addHeader("Content-Type", "application/json");  
				 httpPost.addHeader(key,String.valueOf(value));
			 }
		 }
		  reqParamStr = reqParamStr(reqBodyParams, encoding, reqParamStr);
		 
          httpPost.setConfig(requestConfig);  
          if(reqParamStr!=null){
	           StringEntity stringEntity = new StringEntity(reqParamStr.toString(),(encoding==null||"".equals(encoding)?"utf-8":encoding));//解决中文乱码问题  
	           if(encoding!=null && encoding!=""){
	           		stringEntity.setContentEncoding(encoding);  
	           }
	           if(contentType!=null && contentType!=""){
	           		stringEntity.setContentType(contentType);  
	           }
	           httpPost.setEntity(stringEntity);  
           }
           response = httpClient.execute(httpPost);  
           HttpEntity entity = response.getEntity();  
           if (entity != null && response.getStatusLine().getStatusCode()==200) {
        	   String buffer = IoUtil.getInputStream(entity.getContent());
               return buffer.toString();//返回  
           }else{
           	logger.error("commonHttpClientPost的entity对象为空");
           	return result;
           }
       } catch (IOException e) {
       	logger.error("HttpUrlPost出现异常，异常信息为:"+e.getMessage());
           e.printStackTrace();  
       } finally { 
       	 if(response != null){  
                try {  
               	 response.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
       	 if(httpClient != null){  
                try {  
               	 httpClient.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
       }  
       return result;
	}  
   
	
	
	/** 
     * @Description httpClientGet  
     * @Author      liangjilong  
     * @Date        2017年5月22日 下午3:57:03  
     * @param @param apiUrl
     * @param @param json
     * @param @param contentType
     * @param @return 参数  
     * @return String 返回类型   
     * @throws
     */
    public static String createHttpsGet(String reqUrl, Map<String,Object> reqBodyParams,String contentType,String encoding,Map<String,Object> headerMap) {  
        CloseableHttpClient httpClient = createHttpClient();  
        String httpStr = commonHttpClientGet(reqUrl, reqBodyParams, encoding, headerMap, httpClient);  
        return httpStr;  
    }
    
    /**
     * @Description commonHttpClientGet  
     * @Author      liangjilong  
	 * @Email       jilongliang@sina.com 
     * @Date        2017年8月1日 上午10:35:09  
     * @param reqUrl
     * @param reqBodyParams
     * @param encoding
     * @param headerMap
     * @param httpClient
     * @return 参数  
     * @return String 返回类型
     */
	private static String commonHttpClientGet(String reqUrl, Map<String, Object> reqBodyParams, String encoding, Map<String, Object> headerMap, CloseableHttpClient httpClient) {
		String httpStr = null;  
       
        String reqParamStr = "";
        CloseableHttpResponse response = null;  
        try {  
           
            reqParamStr = reqParamStr(reqBodyParams, encoding, reqParamStr);
            
            HttpGet httpGet = null;
            if(reqParamStr!=null && reqParamStr!=""){
            	httpGet = new HttpGet(reqUrl+"?"+reqParamStr);  
            }else{
            	httpGet = new HttpGet(reqUrl);  
            }
            
            /*设置请求头属性和值 */
    		if(headerMap!=null && !headerMap.isEmpty()){
    			 for (String key : headerMap.keySet()) {
    				 Object value = headerMap.get(key);
    				 //如： httpPost.addHeader("Content-Type", "application/json");  
    				 httpGet.addHeader(key,String.valueOf(value));
    			 }
    		}
            httpGet.setConfig(requestConfig);  
            response = httpClient.execute(httpGet);  
            HttpEntity entity = response.getEntity();  
            if (entity != null && response.getStatusLine().getStatusCode()==200) {
            	logger.info("Response content length: " + entity.getContentLength());
            	httpStr = EntityUtils.toString(entity,encoding);  
            }else{
            	logger.error("commonHttpClientGet的entity对象为空");
            }
        } catch (IOException e) {
        	logger.error("commonHttpClientGet出现异常，异常信息为:"+e.getMessage());
            e.printStackTrace();  
        } finally {  
            if (response != null) {  
                try {  
                    EntityUtils.consume(response.getEntity());  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }
		return httpStr;
	}
    /**
	 * 
     * @Description 获取当前类名包含有包名路径  
     * @Author      liangjilong  
     * @Date        2017年5月24日 上午10:33:49  
     * @param @return 参数  
     * @return String 返回类型   
     * @throws
	 */
	public static String getCurrentClassName(){
		return HttpClientUtil.class.getName();
	}
	
}
